package com.test.nio.demo03;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

public class SelectorSocketChannelDemo {

	static void startClient() throws IOException {
		SocketChannel socketChannel = SocketChannel.open();
		socketChannel.connect(new InetSocketAddress("localhost", 8999));
		socketChannel.configureBlocking(false);
		Selector selector = Selector.open();
		socketChannel.register(selector, SelectionKey.OP_READ);
		new ClientThread(selector).start();

		String request = "hello 我是客户端";
		ByteBuffer buff = ByteBuffer.wrap(request.getBytes("UTF-8"));
		socketChannel.write(buff);
		buff.clear();
	}

	public static void main(String[] args) throws IOException {
		startClient();
	}
}
